package com.eff.android.lib.ui;

import java.util.Locale;

import com.eff.android.lib.R;

import android.app.Dialog;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

public class ECustomDialogHelp extends Dialog 
implements TextToSpeech.OnInitListener{

	TextToSpeech tts;
	ImageButton speaker;
	Context context;
	
	public ECustomDialogHelp(Context context, int theme) {
		super(context, theme);
		
		initViews();
	}
	
	public ECustomDialogHelp(Context context) {
		super(context);

		initViews();
	}
	
	public ECustomDialogHelp(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);

		initViews();
	}
	
	public void initViews(){
		this.setContentView(R.layout.dialog_help);
		this.setTitle("Help");
		this.speaker = (ImageButton) findViewById(R.id.btnSpeaker);
	}

	@Override
	public void onInit(int status) {
		// TODO Auto-generated method stub

		if (status == TextToSpeech.SUCCESS) {

			int result = tts.setLanguage(Locale.US);

			if (result == TextToSpeech.LANG_MISSING_DATA
					|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
				Log.e("TTS", "Language is not supported");
			} else {
				speaker.setEnabled(true);
				SpeakOut("");
			}

		} else {
			Log.e("TTS", "Initilization Failed");
		}

	}
	
	private void SpeakOut(String text){
		tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
	}
	
	public void ProcessClickedEvent(Context ct, String txt){
		tts = new TextToSpeech(ct, this);
		final String text = txt;
	    speaker.setOnClickListener(new View.OnClickListener() {    
	    
		    // Process SpeakOut
			@Override
			public void onClick(View arg0) {
				SpeakOut(text);
			}	
		});		
	}
	
	@Override
	public void dismiss() {
		super.dismiss();
		if (tts != null) {
			tts.stop();
			tts.shutdown();
		}
	}
}
